//
//  CollectionViewController.m
//  Test
//
//  Created by lizq on 16/7/18.
//  Copyright © 2016年 lizq. All rights reserved.
//

#import "CollectionViewController.h"


@interface CollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)NSMutableArray *itemArray;
@property(nonatomic,strong)UICollectionView *collectionView;
@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self factoryData];
    [self setup];

}

- (void)factoryData {
    for(NSInteger i = 0;i< 5; i++) {
        SectionObject *obj = [[SectionObject alloc] init];
        obj.bShow = YES;
        obj.name = [NSString stringWithFormat:@"我是第%ld个Section",i];
        [self.itemArray addObject:obj];
    }
}

- (void)setup {
    [self.view addSubview:self.collectionView];
}

- (UICollectionView *)collectionView {
    if(!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(100, 50);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 20);
        layout.footerReferenceSize = CGSizeMake(0, 0);
        CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        _collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
        _collectionView.backgroundColor= [UIColor clearColor];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        [_collectionView registerClass:[CollectionHeadCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([CollectionHeadCell class])];
        [_collectionView registerClass:[CollectionHeadCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([CollectionHeadCell class])];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    return _collectionView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    SectionObject *obj = [self.itemArray objectAtIndex:section];
    if(obj.bShow) {
        return 10;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.itemArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    CollectionHeadCell *cell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([CollectionHeadCell class]) forIndexPath:indexPath];
    SectionObject *obj = [self.itemArray objectAtIndex:indexPath.item];
    [cell setData:obj];
    cell.refreshBlock = ^{
        [self.collectionView reloadData];
    };
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}


@end

@implementation SectionObject



@end

@interface CollectionHeadCell ()
@property(nonatomic,strong)SectionObject *sectionObj;
@end

@implementation CollectionHeadCell {
    UILabel *nameLabel;
    
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    nameLabel = [[UILabel alloc] init];
    [nameLabel setFrame:self.bounds];
    nameLabel.backgroundColor = [UIColor clearColor];
    
    nameLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSection)];
    [nameLabel addGestureRecognizer:tap];
    
    [self addSubview:nameLabel];
}


- (void)setData:(SectionObject *)obj {
    self.sectionObj = obj;
    nameLabel.text = obj.name;
}

- (void)tapSection {
//    self.sectionObj.bShow = !self.sectionObj.bShow;
////    if(self.sectionObj.bShow) {
////        self.sectionObj.count = 6;
////    }else {
////        self.sectionObj.count = 0;
////    }
    if(self.refreshBlock) {
        self.refreshBlock();
    }
}

@end
